//
//  HXDPlusToolView.swift
//  weibo
//
//  Created by huxuedong on 15/12/14.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit

enum HXDPlusToolViewButtonType: Int {
    case Picture = 0
    case At = 1
    case Talk = 2
    case Emotion = 3
    case Add = 4
}

class HXDPlusToolView: UIStackView {
    
    //定义toolBar按钮的闭包
    var toolViewButtonClickClosure: ((type: HXDPlusToolViewButtonType) -> ())?
    //表情键盘按钮
    private var emoticonButton: UIButton?
    //是否使用表情键盘
    var isEmoticonKeyBoard: Bool = false {
        didSet {
            var defaultName = "compose_emoticonbutton_background"
            if isEmoticonKeyBoard {
                defaultName = "compose_keyboardbutton_background"
            }
            emoticonButton?.setImage(UIImage(named: defaultName), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(defaultName)_highlighted"), forState: UIControlState.Highlighted)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置当前控件子控件排列方向
        axis = .Horizontal
        //设置子控件添加到当前控件的方式为相等填充
        distribution = .FillEqually
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addToolViewButton("compose_toolbar_picture", type: .Picture)
        addToolViewButton("compose_mentionbutton_background", type: .At)
        addToolViewButton("compose_trendbutton_background", type: .Talk)
        emoticonButton = addToolViewButton("compose_emoticonbutton_background", type: .Emotion)
        addToolViewButton("compose_add_background", type: .Add)
    }
    
    private func addToolViewButton(imageName: String, type: HXDPlusToolViewButtonType) -> UIButton {
        let button = UIButton()
        //获取到枚举的原始值(0-4)
        button.tag = type.rawValue
        button.addTarget(self, action: "toolViewButtonClick:", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        //通过图片设置按钮的背景颜色取消高亮状态(也可重写highlighted属性)
        button.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        //添加并布局按钮
        addArrangedSubview(button)
        return button
    }
    
    @objc private func toolViewButtonClick(button: UIButton) {
        //调用闭包，通过原始值获取枚举值，并作为闭包参数
        toolViewButtonClickClosure?(type: HXDPlusToolViewButtonType(rawValue: button.tag)!)
    }

}
